from django.db import models
from barrelhouse.core.locations.models import datacenter
from barrelhouse.core.vendors.models import vendor_account
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class network_transport_type(models.Model):

    '''provides a way to manage types of network POP connections, DS-1, DS-3, OC-3, local ethernet handoff, etc.'''
    name = models.CharField(max_length=50, unique=True, verbose_name="Transport Type", help_text="Type of POP connection")
    notes = models.TextField(help_text="Any unique notes about this type of Network Connection")
    speed = models.IntegerField(help_text="Speed for this transport type")
    speed_rate = models.CharField(max_length=8, help_text="rate for this transport type")

    def speed_info(self):
        return str(self.speed) + " " + str(self.speed_rate)

    def notes_admin(self):
        return self.notes

    notes_admin.allow_tags = True

    def __unicode__(self):
        return unicode(self.speed) + self.speed_rate + " " + self.name

    def get_absolute_url(self):
        return "net_trans_types/%i" % self.id

    class Meta:
        verbose_name = "Network Transport Type"

class network_transport(models.Model):

    '''will eventually be a way to manage network transports / POPs (DSx, OCx, etc.)'''
    name = models.CharField(max_length=50, unique=True, verbose_name="Network Connection", help_text="a leased or provided network POP")
    network_type = models.ForeignKey(network_transport_type, help_text="type of connection")
    vendor = models.ForeignKey(vendor_account, help_text="Account associated with this connection")
    notes = models.TextField(help_text="notes for this network connection")

    def notes_admin(self):
        return self.notes

    notes_admin.allow_tags = True

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "net_trans/%i" % self.id

    class Meta:
        verbose_name = "Network Transport"

class vlan(models.Model):

    number = models.IntegerField(unique=True, help_text="VLAN number")
    notes = models.TextField(null=True, blank=True, help_text="notes for this vlan")

    def __unicode__(self):
        return "VLAN " + unicode(self.number)

    def get_absolute_url(self):
        return "/vlans/%s" % self.number

    def notes_admin(self):
        return self.notes

    notes_admin.allow_tags = True

    class Meta:
        verbose_name = "VLAN"

class network(models.Model):

    name = models.CharField(max_length=45, help_text="short name for network")
    location = models.ForeignKey(datacenter, help_text="geographic location of network")
    connection = models.ForeignKey(network_transport, help_text="the pop for this network")
    vlan = models.ForeignKey(vlan, help_text="vlan for this network")
    subnet = models.IPAddressField(help_text="Subnet for this network")
    subnet_mask = models.IPAddressField(help_text="Subnet Mask for this network")
    notes = models.TextField(help_text="Notes about this network")

    def get_full_network_name(self):
        return "%s %s" % (self.netLocation, self.netName)

    def network_information(self):
        return self.subnet + "/" + self.subnet_mask

    def get_absolute_url(self):
        return "networks/%i/" % self.id

    def __unicode__(self):
        return self.name + "/" + self.subnet_mask

class ip_v4_address(models.Model):
    '''a model to represent all used IPv4 addresses on networks'''
    ip = models.IPAddressField(help_text="associated address", verbose_name="IP Address", unique=True, db_index=True)
    network = models.ForeignKey(network, help_text="Network this IP lives on")
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')

    class Meta:
        verbose_name = "IPv4 Address"
        verbose_name_plural = "IPv4 Addresses"
